/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.Database.ShopArchives;
import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * Archives表操作
 * @author lcfgly
 */
@Repository
public class ArchiveImpl implements Serializable{
    
    private final String table="shop_archives";
    private final String sqlheader="SELECT id,title,writer,description,updatetime,writeruid,body,typeid FROM shop_archives ";
    
    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    /**
     * 更新数据
     * @param arg0
     * @return
     * @throws SQLException 
     */
    @UpdateCacheMark("shoparchivesbyid_${0.id}")
    public int update(ShopArchives arg0) throws SQLException {
        return dataFactory.update(table,arg0, "id=?", arg0.getId());
    }
    
    /**
     * 批量更新
     * @param args
     * @return
     * @throws SQLException 
     */
    public int[] update(List<ShopArchives> args) throws SQLException {
        int[] result=new int[args.size()];
        int i=0;
        for(ShopArchives arg:args){
            result[i++]=update(arg);
        }
        return result;
    }
    
    /**
     * 按照ID查询
     * @param id
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shoparchivesbyid_${0}")
    public ShopArchives findById(int id) throws SQLException
    {
        String sql=sqlheader+" WHERE id=?";
        return dataFactory.one(ShopArchives.class,sql, id);
    }
    
    /**
     * 根据作者和ID删除文章
     * @param id
     * @param userid
     * @return
     * @throws SQLException 
     */
    public int delbyIdAndUserid(int id,int userid)throws SQLException{
        return dataFactory.delete(table,"id=? AND writeruid =? ",id,userid);
    }
    
    /**
     * 插入数据
     * @param archives
     * @return
     * @throws SQLException 
     */
    public int insert(ShopArchives archives)throws SQLException{
        return dataFactory.insert(archives, table);
    }
    
    /**
     * 获取文章及分页
     * @param shopid
     * @param typeid
     * @param page
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopGetArchivesForPage_${0}_${1}_${2}")
    public Map GetArchivesForPage(int shopid,int typeid, int page) throws SQLException{
        String sql="SELECT id,title,updatetime FROM "+table+" WHERE writeruid=? AND typeid=? Order by updatetime desc";
        String s="SELECT count(*) FROM "+table+" WHERE writeruid=? AND typeid=?";
        int rowsnum=dataFactory.Getrowsnum(s, shopid,typeid);
        return dataFactory.Page(null, sql, rowsnum, page, 20, shopid,typeid);
    }
    
}
